/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.util.ImageUtil;

/**
 * @author ETCORE GroupTalk Developers
 * 
 */
public class CutAvatarAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8628339677729933578L;
	public static final int ACCESS_ALLOWED = 1;
	public static final int ACCESS_ERROR = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#enablePostLoad()
	 */
	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#execute(int,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		if (ACCESS_ALLOWED == validationResult) {

			try {
				cutAvatar(request);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	public void cutAvatar(HttpServletRequest request) throws IOException {

		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute("session_user");
		if (user != null) {

			String lang = (String) session.getAttribute("lang");

			try {
				String cutPos = request.getParameter("cut_pos");
				String[] pos = cutPos.split(",");

				int x = Integer.parseInt(pos[0]);
				int y = Integer.parseInt(pos[1]);

				int picWidth = Integer.parseInt(pos[2]);
				int picHeight = Integer.parseInt(pos[3]);
				String webInfPath = this.getServletConfig().getServletContext()
						.getRealPath("WEB-INF");
				String rootWebPath = webInfPath.substring(0,
						webInfPath.indexOf("WEB-INF"));

				// String tempPath = rootWebPath +
				// PageConstants.UPLOAD_TEMP_PATH;
				String srcPath = rootWebPath + user.getAvatar();

				ImageUtil.ZoomImage(srcPath, srcPath, picWidth, picHeight);
				ImageUtil.cut(srcPath, srcPath, x, y, 200, 200);

				String msg = null;
				if (lang != null && lang.equals(Messages.LANG_EN)) {
					msg = Messages.getELabel(Messages.MSG_SUCCESS);
				} else {
					msg = Messages.getLabel(Messages.MSG_SUCCESS);
				}
				request.setAttribute(Messages.MSG, msg);
			} catch (Exception e) {

				String msg = null;
				if (lang != null && lang.equals(Messages.LANG_EN)) {
					msg = Messages.getELabel(Messages.MSG_UPLOAD_ERROR);
				} else {
					msg = Messages.getLabel(Messages.MSG_UPLOAD_ERROR);
				}
				request.setAttribute(Messages.MSG, msg);

				e.printStackTrace();
				return;
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#getForward(javax
	 * .servlet.http.HttpServletRequest, int)
	 */
	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#getRedirect(
	 * javax.servlet.http.HttpServletRequest, int)
	 */
	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		return PageConstants.PAGE_ACTION_MYINFO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#isForward(int)
	 */
	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * etcore.webapp.grouptalk.controller.action.AbstractAction#validate(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
		if (user != null)
			return ACCESS_ALLOWED;
		return ACCESS_ERROR;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
